package web

import (
	"fmt"
	"ksd-social-api/commons/base/controller"
	"ksd-social-api/modules/note/model/context"
	"ksd-social-api/utils/rdb"
	"strconv"
)

type NoteController struct {
	controller.BaseController
}

/**
 * 购买认证
 * @author feige
 * @date 2024-03-26
 * @version 1.0
 * @desc
 */
func (web *NoteController) BuyValidator() {

}

/**
 * 上一篇下一篇
 * @author feige
 * @date 2024-04-17
 * @version 1.0
 * @desc
 */
func (web *NoteController) FindNotePrevNext() {
	noteId, _ := web.ParseParamUint64("noteId")
	pvo, _ := noteService.GetNoteByIDToPay(noteId, web.GetSystemId())
	if pvo.CategoryId > 0 {
		data, _ := noteService.FindNotePrevNext(pvo.CategoryId, web.GetSystemId())
		// 3：执行查询返回
		web.Ok(data)
		return
	}
	web.Ok("")
}

func (web *NoteController) UpdateNoteVideoId() {
	noteVideoContext := context.NoteVideoContext{}
	// 2: 开始绑定参数
	web.BindJSON(&noteVideoContext)
	noteVideoContext.SystemId = web.GetSystemId()
	// 3: 删除缓存
	key := fmt.Sprintf("SYSTEM:SPECIAL:GetUserStudyDetailByPath:%s:%d", noteVideoContext.SpecialId, noteVideoContext.SystemId)
	cacheKey := fmt.Sprintf("SYSTEM:NOTE:GetNoteDetailByID:%s:%d", noteVideoContext.Id.Data, noteVideoContext.SystemId)
	rdb.RdbDel(key)
	rdb.RdbDel(cacheKey)
	// 3：执行查询返回
	web.Ok(noteService.UpdateNoteVideoId(&noteVideoContext))
}

func (web *NoteController) UpdateNoteVideo() {
	noteVideoContext := context.NoteVideoContext{}
	// 2: 开始绑定参数
	web.BindJSON(&noteVideoContext)
	noteVideoContext.SystemId = web.GetSystemId()
	// 3: 删除缓存
	key := fmt.Sprintf("SYSTEM:SPECIAL:GetUserStudyDetailByPath:%s:%d", noteVideoContext.SpecialId, noteVideoContext.SystemId)
	cacheKey := fmt.Sprintf("SYSTEM:NOTE:GetNoteDetailByID:%d:%d", noteVideoContext.Id.Data, noteVideoContext.SystemId)
	rdb.RdbDel(key)
	rdb.RdbDel(cacheKey)
	// 3：执行查询返回
	web.Ok(noteService.UpdateNoteVideo(&noteVideoContext))
}

/*
* 保存课程笔记
 */
func (web *NoteController) SaveUpdateNoteVideo() {
	noteContext := context.NoteContext{}
	// 2: 开始绑定参数
	web.BindJSON(&noteContext)
	noteContext.SystemId = web.GetSystemId()
	noteContext.SystemName = web.GetSystemName()
	noteContext.Userid = web.GetUserId()
	noteContext.Username = web.GetUsername()
	noteContext.Uuid = web.GetUuid()
	noteContext.Avatar = web.GetUserAvatar()
	noteContext.ClientLink = "ccxiaoli2024"
	noteContext.ClientLinkName = "ccxiaoli2024"
	noteContext.ClientLinkImg = "https://ksd-social.oss-cn-shenzhen.aliyuncs.com/clients/2024/04/13/7e4636ac-8e9e-4d9b-8799-3b6c06b0af61.jpg"
	noteContext.QqLinkCode = "1571828260"
	noteContext.QqLinkImg = "1571828260"
	noteContext.WeixinLinkName = "xuchengfeifei"
	noteContext.WeixinLinkImg = "https://ksd-social.oss-cn-shenzhen.aliyuncs.com/clients/2024/04/13/7948417a-8d5c-4c88-b423-e4a047e16c64.jpg"
	noteContext.ClientFlag = "1"
	noteContext.Weixingzcode = "源码星球"
	noteContext.Weixingzimg = "https://ksd-social.oss-cn-shenzhen.aliyuncs.com/notes/a5db42480b6e91fd9cf54519543d5dbd/0fe11d9f10b54db09ac56487d8c49803.jpg"
	noteContext.Opengz = "1"
	noteContext.Otherinfo = ""
	// 3：执行查询返回
	noteId := noteService.SaveUpdateNoteVideo(&noteContext)
	web.Ok(strconv.FormatUint(noteId, 10))
}

func (web *NoteController) DeleteNoteCache() {
	noteId, _ := web.ParseParamUint64("noteId")
	cacheKey1 := fmt.Sprintf("SYSTEM:NOTE:GetNoteDetailByID:%d:%d", noteId, web.GetSystemId())
	cacheKey2 := fmt.Sprintf("SYSTEM:SPECIAL:GetUserStudyDetailByPath:%s:%d", "projects", web.GetSystemId())
	cacheKey3 := fmt.Sprintf("SYSTEM:SPECIAL:GetSpecialByPath:%s:%d", "projects", web.GetSystemId())
	rdb.RdbDel(cacheKey1)
	rdb.RdbDel(cacheKey2)
	rdb.RdbDel(cacheKey3)
	// 3：执行查询返回
	web.Ok("success")
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据小册id查询明细---编辑使用
 */
func (web *NoteController) GetDetail() {
	// 1: 获取小册id
	noteId, _ := web.ParseParamUint64("id")
	// 浏览数递增
	ipAddr := web.GetIpAddr()
	// 3: 开始查询小册的信息
	vo, err := noteService.GetNoteByID(noteId, web.GetSystemId(), true)
	// 如果查询出错，把错误信息返回。方便定位错误
	if err != nil {
		web.FailCodeMsg(701, "查询失败，原因是：没有此内容")
		return
	}
	noteService.UpdateViewCount(noteId, ipAddr, web.GetSystemId())
	web.Ok(vo)
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据小册id查询明细---编辑使用
 */
func (web *NoteController) SaveCopy() {
	// 1: 获取小册id
	noteId, _ := web.ParseParamUint64("noteId")
	newNoteId := noteService.CopyNote(noteId, web.GetSystemId())
	web.Ok(fmt.Sprintf("%d", newNoteId))
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据小册id查询明细---没有章节
 */
func (web *NoteController) GetDetailNoChapter() {
	// 1: 获取小册id
	noteId, _ := web.ParseParamUint64("id")
	// 浏览数递增
	ipAddr := web.GetIpAddr()
	// 3: 开始查询小册的信息
	vo, err := noteService.GetNoteByID(noteId, web.GetSystemId(), false)
	// 如果查询出错，把错误信息返回。方便定位错误
	if err != nil {
		web.FailCodeMsg(701, "查询失败，原因是：没有此内容")
		return
	}
	noteService.UpdateViewCount(noteId, ipAddr, web.GetSystemId())
	web.Ok(vo)
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据小册id查询明细---查看和学习使用
 */
func (web *NoteController) GetDetailAll() {
	// 1: 获取小册id
	noteId, _ := web.ParseParamUint64("id")
	// 2: 用户id从哪里获取
	userId := web.GetUserId()
	// 浏览数递增
	ipAddr := web.GetIpAddr()
	// 3: 开始查询小册的信息
	vo, err := noteService.GetNoteDetailByID(noteId, userId, web.GetSystemId())
	// 如果查询出错，把错误信息返回。方便定位错误
	if err != nil {
		web.FailCodeMsg(701, "查询失败，原因是：没有此内容")
		return
	}

	if vo.IsDeleted == 1 {
		web.FailCodeMsg(701, "找不到你想要的内容！")
		return
	}

	if vo.Status == 0 {
		web.FailCodeMsg(701, "作者还在撰写中，尚未发布！")
		return
	}

	// 保存用户的文章浏览记录
	if web.GetUserId() > 0 {
		noteUserHitContext := context.NoteUserHitsContext{}
		noteUserHitContext.Opid = noteId
		noteUserHitContext.UserId = web.GetUserId()
		noteUserHitContext.SystemId = web.GetSystemId()
		noteUserHitContext.SystemName = web.GetSystemName()
		noteUserHitContext.Username = web.GetUsername()
		noteUserHitContext.Nickname = web.GetUserNickname()
		noteUserHitContext.Avatar = web.GetUserAvatar()
		noteUserHitContext.Uuid = web.GetUuid()
		noteService.SaveUserNoteHits(&noteUserHitContext)
	}
	// 浏览数+1
	noteService.UpdateViewCount(noteId, ipAddr, web.GetSystemId())
	web.Ok(vo)
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc  返回小册分页信息（根据栏目查询）
 */
func (web NoteController) FindNoteListAndPage() {
	// 1: 定义一个接受分页信息的上下文
	noteSearchContext := context.NoteSearchContext{}
	// 2: 开始绑定参数
	web.BindJSON(&noteSearchContext)
	noteSearchContext.SystemId = web.GetSystemId()
	// 3：执行查询返回
	web.Ok(noteService.FindNoteListAndPage(noteSearchContext))
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc  点赞小册
 */
func (web NoteController) LikeNote() {
	noteId, _ := web.ParseParamUint64("noteId")
	// 3：执行查询返回
	flag, err := noteService.LikeNote(web.GetUserId(), noteId, web.GetSystemId())
	if err != nil {
		web.FailCodeMsg(901, err.Error())
	} else {
		web.Ok(flag)
	}
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc  取消点赞
 */
func (web NoteController) UnLikeNote() {
	noteId, _ := web.ParseParamUint64("noteId")
	// 3：执行查询返回
	flag, err := noteService.UnLikeNote(web.GetUserId(), noteId, web.GetSystemId())
	if err != nil {
		web.FailCodeMsg(901, err.Error())
	} else {
		web.Ok(flag)
	}
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc  收藏小册
 */
func (web NoteController) FavNote() {
	noteId, _ := web.ParseParamUint64("noteId")
	// 3：执行查询返回
	flag, err := noteService.FavNoteByUserId(web.GetUserId(), noteId, web.GetSystemId())
	if err != nil {
		web.FailCodeMsg(901, err.Error())
	} else {
		web.Ok(flag)
	}
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc 取消小册收藏
 */
func (web NoteController) UnFavNote() {
	noteId, _ := web.ParseParamUint64("noteId")
	// 3：执行查询返回
	flag, err := noteService.UnFavNoteByUserId(web.GetUserId(), noteId, web.GetSystemId())
	if err != nil {
		web.FailCodeMsg(901, err.Error())
	} else {
		web.Ok(flag)
	}
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc  返回推荐的小册信息
 */
func (web NoteController) FindPushNoteListAndPage() {
	// 1: 定义一个接受分页信息的上下文
	noteSearchContext := context.NoteSearchContext{}
	// 2: 开始绑定参数
	web.BindJSON(&noteSearchContext)
	noteSearchContext.SystemId = web.GetSystemId()
	// 3：执行查询返回
	web.Ok(noteService.FindPushNoteListAndPage(noteSearchContext))
}

/**
 * @author feige
 * @date 2023-10-17
 * @version 1.0
 * @desc 统计文章发布
 */
func (web *NoteController) CountNoteAll() {
	channelId, _ := web.ParseParamInt64("channelId")
	web.Ok(noteService.CountNoteAll(channelId))
}
